﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using LRSTaxServices.DAL;
using LRSTaxServices.DAL.dsClientSupportTableAdapters;
using LRSTaxServices.Common;

namespace LRSTaxServices.Biz
{
    /// <summary>
    /// ClientSupport Business Object
    /// </summary>

    public class ClientSupport : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected ClientSupport() { }

        #endregion

        #region Public Properties

        #region Instance Properties

        /// <summary>
        /// PKID of the ClientSupport.
        /// </summary>
        public int ClientSupportID
        {
            get { return _ClientSupportID; }
            set
            {
                if (_ClientSupportID != value)
                {
                    if (OnPropertyChanging("ClientSupportID", _ClientSupportID, value))
                    {
                        int oldValue = _ClientSupportID;
                        _ClientSupportID = value;

                        OnPropertyChanged("ClientSupportID", oldValue, value);
                    }
                }
            }
        }  private int _ClientSupportID = -1;




        /// <summary>
        /// FirstName.
        /// </summary>
        public string FirstName
        {
            get { return _firstname; }
            set
            {
                if (_firstname != value)
                {
                    if (OnPropertyChanging("FirstName", _firstname, value))
                    {
                        string oldValue = _firstname;
                        _firstname = value;
                        OnPropertyChanged("FirstName", oldValue, value);
                    }
                }
            }
        }  private string _firstname = null;



        /// <summary>
        /// LastName.
        /// </summary>
        public string LastName
        {
            get { return _lastname; }
            set
            {
                if (_lastname != value)
                {
                    if (OnPropertyChanging("LastName", _lastname, value))
                    {
                        string oldValue = _lastname;
                        _lastname = value;
                        OnPropertyChanged("LastName", oldValue, value);
                    }
                }
            }
        }  private string _lastname = null;



        /// <summary>
        /// Contact Phone
        /// </summary>
        public string ContactPhone
        {
            get { return _contactphone; }
            set
            {
                if (_contactphone != value)
                {
                    if (OnPropertyChanging("ContactPhone", _contactphone, value))
                    {
                        string oldValue = _contactphone;
                        _contactphone = value;
                        OnPropertyChanged("ContactPhone", oldValue, value);
                    }
                }
            }
        }  private string _contactphone = null;



        /// <summary>
        /// EmailAddress.
        /// </summary>
        public string EmailAddress
        {
            get { return _emailaddress; }
            set
            {
                if (_emailaddress != value)
                {
                    if (OnPropertyChanging("EmailAddress", _emailaddress, value))
                    {
                        string oldValue = _emailaddress;
                        _emailaddress = value;
                        OnPropertyChanged("EmailAddress", oldValue, value);
                    }
                }
            }
        }  private string _emailaddress = null;



        /// <summary>
        /// Question.
        /// </summary>
        public string Question
        {
            get { return _question; }
            set
            {
                if (_question != value)
                {
                    if (OnPropertyChanging("Question", _question, value))
                    {
                        string oldValue = _question;
                        _question = value;
                        OnPropertyChanged("Question", oldValue, value);
                    }
                }
            }
        }  private string _question = null;



        /// <summary>
        /// Date/time at which the ClientSupport was created.
        /// </summary>
        public DateTime? AddedDate
        {
            get { return _addedDate; }
            set
            {
                if (_addedDate != value)
                {
                    if (OnPropertyChanging("AddedDate", _addedDate, value))
                    {
                        DateTime? oldValue = _addedDate;
                        _addedDate = value;
                        OnPropertyChanged("AddedDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _addedDate = null;






        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty JobCategory object.
        /// </summary>
        public static ClientSupport CreateNew()
        {
            ClientSupport obj = new ClientSupport();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;
            obj.AddedDate = DateTime.Now;

            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static ClientSupport CreateNew(ClientSupportCollection collection)
        {
            ClientSupport obj = CreateNew();

            // Get next/unique ID for the new object.
            int minVal = 0;
            foreach (ClientSupport ClientSupport in collection)
            {
                if (ClientSupport.ClientSupportID < minVal)
                    minVal = ClientSupport.ClientSupportID;
            }
            obj.ClientSupportID = --minVal;
            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetAllClientSupport

        /// <summary>
        /// Returns a collection of all ClientSupport.
        /// </summary>
        public static ClientSupportCollection GetAllClientSupport()
        {
            ClientSupportTableAdapter adapter = new ClientSupportTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsClientSupport.ClientSupportDataTable tbl = adapter.GetData(null);

            ClientSupportCollection list = new ClientSupportCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsClientSupport.ClientSupportRow row in tbl.Rows)
                {
                    ClientSupport ClientSupport = new ClientSupport();
                    RelationalToObject(ClientSupport, row);
                    list.Add(ClientSupport);
                }
            }

            return (list);
        }

        #endregion

        #region GetClientSupportByID

        /// <summary>
        /// Returns an ClientSupport object for the specified 'ClientSupportID' or null if no ClientSupport with the specified parameter(s) exists.
        /// </summary>
        public static ClientSupport GetClientSupportByID(int ClientSupportid)
        {
            ClientSupportTableAdapter adapter = new ClientSupportTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsClientSupport.ClientSupportDataTable tbl = adapter.GetData(ClientSupportid);

            if (tbl.Rows.Count == 1)
            {
                ClientSupport ClientSupport = new ClientSupport();
                RelationalToObject(ClientSupport, tbl.Rows[0]);
                return (ClientSupport);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'ClientSupport' returned more than one User record.");

            return (null);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified ClientSupport.
        /// </summary>
        public static void Update(ClientSupport obj)
        {
            bool updateClientSupport = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateClientSupport = (obj.ObjState != ObjectState.Unchanged);

                if (!updateClientSupport)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsClientSupport.ClientSupportDataTable tbl = null;
            if (updateClientSupport)
            {
                ClientSupportTableAdapter adapter = new ClientSupportTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsClientSupport.ClientSupportDataTable();
                dsClientSupport.ClientSupportRow row = tbl.NewClientSupportRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted) && (ClientSupportID <= 0))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("ClientSupportID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

                // FirstName is required.
                if ((FirstName == null) || (FirstName.Trim().Length == 0))
                {
                    Errors.Add(new ValidationError("FirstName", "FirstName is required.", ValidationError.Severity.Error));
                }
                else if (ObjState == ObjectState.Added)
                {

                }

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("ClientSupport: ClientSupportID={0}, EmailAddress={1}, {2}", ClientSupportID, FirstName, EmailAddress));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more ClientSupport objects.
    /// </summary>

    public class ClientSupportCollection : List<ClientSupport>
    {
        #region Types

        public class UserComparer : IComparer<ClientSupport>
        {
            public int Compare(ClientSupport q1, ClientSupport q2)
            {
                return (String.Compare(q1.FirstName, q2.FirstName));
            }
        }

        #endregion

        #region Public Methods

        #region FindByClientSupportID

        public ClientSupport FindByClientSupportID(int ClientSupportID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].ClientSupportID == ClientSupportID)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #region FindClientSupportByFirstName

        /// <summary>
        /// Case insensitve compare.
        /// </summary>
        public ClientSupport FindClientSupportByFirstName(string firstname)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].FirstName == firstname)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion
    }
}
